@tool
class_name AdditionalEffect_AddHitLogic
extends AdditionalEffect

## 添加Hit逻辑

#region 属性
@export var detector_id:int
## 目标数量，只有在检测到target_count数量的目标时才触发HitLogic
@export var target_count:int = 1
## 是否在触发HitLogic后清空target_count记录
@export var is_clear_target_count:bool = false
@export var connect_flag:ConnectFlags
@export var hit_additional_effects:Array[AdditionalEffect]
var cur_target_count:int = 0
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
## 初始化
func init(args:Dictionary = {}):
	if super(args):
		_reset()
		return true
	return false
## 检查能否执行添加效果，默认为true
func can_excute() -> bool:
	return true
## 执行添加效果
func excute() -> bool:
	if super():
		var combat_component:CombatComponent = target.get_component(CombatComponent)
		if combat_component:
			var hit_box:HitBox = combat_component.get_hit_box()
			if hit_box:
				var hitBoxAreaDetector:HitBoxAreaDetector = hit_box.get_detector(detector_id)
				if hitBoxAreaDetector:
					hitBoxAreaDetector.set_on_detect_target(_on_detect_target,true,connect_flag)
		return true
	else:
		return false
## 取消执行的效果
func cancel_excute() -> bool:
	if super():
		var combat_component:CombatComponent = target.get_component(CombatComponent)
		if combat_component:
			var hit_box:HitBox = combat_component.get_hit_box()
			if hit_box:
				var hitBoxAreaDetector:HitBoxAreaDetector = hit_box.get_detector(detector_id)
				if hitBoxAreaDetector:
					hitBoxAreaDetector.set_on_detect_target(_on_detect_target,false,connect_flag)
		return true
	else:
		return false
#endregion
#region 公共方法
#endregion
#region 私有方法
func _reset():
	cur_target_count = target_count
## 碰撞到的对象不一定是Entity，还可能是TileMapLayer和其它碰撞体
func _on_detect_target(target):
	cur_target_count += 1
	if cur_target_count < target_count:
		return
	var clone_effects: = hit_additional_effects.duplicate(true)
	Utils.array_call_method(clone_effects,"init",{Target = target,Owner = owner})
	Utils.array_call_method(clone_effects,"excute")
	if is_clear_target_count:
		_reset()
#endregion
#region 生命周期函数
func _init() -> void:
	if Engine.is_editor_hint():
		resource_local_to_scene = true
#endregion
